home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 273_01.zip / TCMENU.CC < prev    next >
Text File  |  1993-04-04  |  5KB  |  213 lines

  1. #include <tcutil.h>
  2. #include <tcmenu.h>
  3. #include <string.h>
  4.  
  5.  
  6.  
  7.  
  8. menubar(struct menu_struc *menu1, struct pull_down *pulls)
  9. /*     This function will display a menu-bar at the top of the screen.
  10.     Before calling this function you must set up the two menu stuctures
  11.     needed my the function. The two structures are:
  12.     1) menu_struc
  13.     2) pull_down
  14.     The structures are defined in tcmenu.h.  To initialize do the following:
  15.  
  16.     struct menu_struc menu = {4,setatr(WHITE,BLUE,BOLD,0),
  17.                             "Application\0Gen\0Help\0Last\0"};
  18.  
  19.     item-1 = number of menu selections.
  20.     item-2 = attr of menu bar.
  21.     item-3 = string of menu items seperated by a null character.
  22.  
  23.     struct pull_down pulls[] = {4,20,"app1\0app2\0app3\0app4\0",
  24.                                 4,20,"gen1\0gen2\0gen3\0gen4\0",
  25.                                 4,20,"help1\0help2\0help3\0help4\0",
  26.                                 10,20,"l1\0l2\0l3\0l4\0l5\0l6\0l7\0l8\0l9\0la\0"};
  27.  
  28.     Define structure pull_down with the same number of pull down menus as there
  29.     are menu items.
  30.  
  31.     item-1 = number of pull down selections.
  32.     item-2 = length of the biggest pull down selection.
  33.     item-3 = string of pull down selections seperated by a null character.
  34.  
  35.     To call the function do as follows:
  36.  
  37.     item_num=menubar(&menu,&pulls[0]);
  38.  
  39.     return = 0 if no items where selected.
  40.     return !=0 if an item was selected.
  41.                (the returned value will = the number of the menu item
  42.                selected. I.E. 1-x;
  43.  
  44.     ALSO menu.selected will = the main menu item number selected and
  45.          menu.p_sel will = the pull down item number under that main menu item.
  46.  
  47. */
  48.  
  49. {
  50. int attr2, attr3;
  51. int p, i, start, interval;
  52. int x, key_val, rc, rc2;
  53. int cur_state, orow, ocol;
  54. char *items, save_1[160];
  55. static int first = 1;
  56.     if(first) {
  57.         video_type();
  58.         first=0;
  59.     }
  60.     items=menu1->menu_string;
  61.     cur_state=get_cur(&orow,&ocol);
  62.     hide_cur();
  63.     attr2=_rotr(menu1->attr,4) & 0x0007;
  64.     attr3=_rotl(menu1->attr,4) & 0x0070;
  65.     attr2 = attr2 | attr3 | 0x0008;
  66.     menu1->rattr=attr2;
  67.     save_scr(0,0,0,79,&save_1);
  68.     x=0;
  69.     clrarea(0,0,0,79,menu1->attr);
  70.  
  71.     interval=80 / menu1->nsels;
  72.  
  73.     i = 0;
  74.     start=1;
  75.     for(p=0; p < menu1->nsels; p++) {
  76.         menu1->icol[x]=start;
  77.         menu1->ilen[x]=strlen(items);
  78.         x++;
  79.         writef(0,start,menu1->attr,items);
  80.         i = strlen(items) + 1;
  81.         items += i;
  82.         start += interval;
  83.     }
  84.     x=0;
  85.     rc2=0;
  86.     for(;;) {
  87.         rcolor(0,menu1->icol[x],attr2,menu1->ilen[x]);
  88.         if(rc2 == 0)
  89.             key_val=get_xa();
  90.         else  {
  91.             key_val=rc2;
  92.             if(rc2 == K_ENTER) rc2=0;
  93.         }
  94.         switch (key_val) {
  95.             case K_ESC:
  96.                 rc=0;
  97.                 menu1->selected = 0;
  98.                 menu1->p_sel=0;
  99.                 goto pass_back;
  100.             case K_RIGHT:
  101.                 rcolor(0,menu1->icol[x],menu1->attr,menu1->ilen[x]);
  102.                 x++;
  103.                 if(x == menu1->nsels) x=0;
  104.                 if(rc2 == K_RIGHT) rc2=K_ENTER;
  105.                 break;
  106.             case K_LEFT:
  107.                 rcolor(0,menu1->icol[x],menu1->attr,menu1->ilen[x]);
  108.                 x--;
  109.                 if(x < 0) x = menu1->nsels -1;
  110.                 if(rc2 == K_LEFT) rc2=K_ENTER;
  111.                 break;
  112.             case K_ENTER:
  113.                 rc=x + 1;
  114.                 menu1->selected = rc;
  115.                 rc2=do_pull_down(pulls+x,menu1);
  116.                 if(rc2 == K_ESC) {
  117.                     rc2 = 0;
  118.                     break;
  119.                 }
  120.                 if(rc2 != K_ENTER) break;
  121.                 goto pass_back;
  122.             default: break;
  123.         }
  124.     }
  125. pass_back:
  126.     rest_scr(0,0,0,79,&save_1);
  127.     if(!cur_state) show_cur(1);
  128.     locate(orow,ocol);
  129.     return(rc);
  130. }
  131.  
  132.  
  133. do_pull_down(struct pull_down *pull, struct menu_struc *menu1)
  134. {
  135. int p, i, start;
  136. int x, key_val, rc;
  137. int wleft, wright, wbottom;
  138. int y;
  139. char *save_2;
  140. char *items;
  141.     save_2=(char *)malloc(4000);
  142.     y = menu1->selected - 1;
  143.     items=pull->pull_items;
  144.     x=0;
  145.     if(menu1->icol[y] > 39) {
  146.         wright=menu1->icol[y] + menu1->ilen[y];
  147.         wleft=wright - pull->mlen - 2;
  148.     }
  149.     else {
  150.         wleft=menu1->icol[y] - 1;
  151.         wright=wleft + pull->mlen + 2;
  152.     }
  153.  
  154.     wbottom=2 + pull->npulls;
  155.  
  156.     save_scr(1,wleft,wbottom,wright,save_2);
  157.     make_window(1,wleft,wbottom,wright,menu1->attr,menu1->attr,0,"","");
  158.     if(menu1->icol[y] > 39) {
  159.         writefc(1,wright,menu1->attr,'\xb4');
  160.         writefc(1,wright - menu1->ilen[y] - 1,menu1->attr,'\xc1');
  161.     }
  162.     else {
  163.         writefc(1,wleft,menu1->attr,'\xc3');
  164.         writefc(1,wleft + menu1->ilen[y] + 1,menu1->attr,'\xc1');
  165.     }
  166.  
  167.     i = 0;
  168.     start = 2;
  169.     for(p=0; p < pull->npulls; p++) {
  170.         writef(start,wleft + 1,menu1->attr,items);
  171.         i = strlen(items) + 1;
  172.         items += i;
  173.         start ++;
  174.     }
  175.  
  176.     x=1;
  177.     for(;;) {
  178.         rcolor(x+1,wleft+1,menu1->rattr,pull->mlen);
  179.         key_val=get_xa();
  180.         switch (key_val) {
  181.             case K_ESC:
  182.                 menu1->p_sel = 0;
  183.                 rc=key_val;
  184.                 goto pass_back;
  185.             case K_DOWN:
  186.                 rcolor(x+1,wleft+1,menu1->attr,pull->mlen);
  187.                 x++;
  188.                 if(x > pull->npulls) x=1;
  189.                 break;
  190.             case K_UP:
  191.                 rcolor(x+1,wleft+1,menu1->attr,pull->mlen);
  192.                 x--;
  193.                 if(x < 1) x = pull->npulls;
  194.                 break;
  195.             case K_ENTER:
  196.                 menu1->p_sel = x;
  197.                 rc=key_val;
  198.                 goto pass_back;
  199.             case K_RIGHT:
  200.             case K_LEFT:
  201.                 menu1->p_sel = 0;
  202.                 rc=key_val;
  203.                 goto pass_back;
  204.         }
  205.     }
  206. pass_back:
  207.     rest_scr(1,wleft,wbottom,wright,save_2);
  208.     free(save_2);
  209.     return(rc);
  210. }
  211.  
  212.  
  213.